package com.timber.util;

/**
 /**
  * Requires:
  * "xreport_pro.jar" and "xtools.jar" to be in classpath or
  * setup in JBuilder Project->Proejct Properties->Paths(Tab)->Required Libraries
  * on Windows platform source/java/lib/Win32Printer.dll should be in PATH
  *
  */

 /**
  * NOTE:
  * <pre>
  *       There are 2 principally different ways of using SLTablePrinter class:
  *       with "late binding" or "hard binding"
  *              Late binding - means that the latest table data model of given TableView
  *                             will be used as input.
  *                   Hence _tableView interface constructors/factory methods should be used
  *
  *              Hard Binding - means that table model set at constructor call time will be used
  *                             for printing
  *                   Hence _tableModel interface constructors/factory methods should be used
  *
  *
  * Usage exemple:
  *
  * Late Binding
  *
  * 1. Prints given table providing the dialogs for paper orientation/printer selection
  *
  *     this.addActionListener(new java.awt.event.ActionListener() {
  *          public void actionPerformed(ActionEvent e) {
  *            SLTablePrinter.createTablePrinter(_tableView, _title).printWithDialog();
  *          }
  *     });
  *
  * 2. Prints given table in background with default values for paper orientation(PORTRAIT)/printer
  *
  *     this.addActionListener(new java.awt.event.ActionListener() {
  *          public void actionPerformed(ActionEvent e) {
  *            SLTablePrinter.createTablePrinter(_tableView, _title).print();
  *          }
  *     });
  *
  * 3. Prints given table in background with LANSCAPE paper orientation on default printer
  *
  *     this.addActionListener(new java.awt.event.ActionListener() {
  *          public void actionPerformed(ActionEvent e) {
  *            SLTablePrinter.createTablePrinter(_tableView, _title, SLTablePrinter.LANDSCAPE).print();
  *          }
  *     });
  *
  *
  * 4. Manually Sets up the printer object
  *
  *     SLTablePrinter aPrinter = new SLTablePrinter(_tabelView, _Title);
  *     aPrinter.setOrientation(SLTablePrinter.LANDSCAPE); // or SLTablePrinter.PORTRAIT, or SLTablePrinter.REVERSE_LANDSCAPE
  *     aPrinter.setMargins(0.75, 0.75, 0.75, 0.75);
  *     //Format per Column
  *     aPrinter.addFormat( PMColumnManager.COLLATERAL, new DecimalFormat("#,###"));
  *     aPrinter.addFormat( PMColumnManager.REBATE, new DecimalFormat("#,##0.0000"));
  *     aPrinter.addFormat( PMColumnManager.FUND_RATE, new DecimalFormat("#,##0.0000"));
  *     aPrinter.print();
  *
  * 5. SLTablePrinter aPrinter = new SLTablePrinter(_tabelView, _Title, _Orientation);
  *
  *
  * Hard Binding:
  *
  * 6. Prints given table providing the dialogs for paper orientation/printer selection
  *
  *     this.addActionListener(new java.awt.event.ActionListener() {
  *          public void actionPerformed(ActionEvent e) {
  *            SLTablePrinter.createTablePrinter(_tableModel, _title).printWithDialog();
  *          }
  *     });
  *
  * 7. Prints given table in background with default values for paper orientation(PORTRAIT)/printer
  *
  *     this.addActionListener(new java.awt.event.ActionListener() {
  *          public void actionPerformed(ActionEvent e) {
  *            SLTablePrinter.createTablePrinter(_tableModel, _title).print();
  *          }
  *     });
  *
  * 8. Prints given table in background with LANSCAPE paper orientation on default printer
  *
  *     this.addActionListener(new java.awt.event.ActionListener() {
  *          public void actionPerformed(ActionEvent e) {
  *            SLTablePrinter.createTablePrinter(_tableModel, _title, SLTablePrinter.LANDSCAPE).print();
  *          }
  *     });
  *
  *
  * 9. Manually Sets up the printer object
  *
  *     SLTablePrinter aPrinter = new SLTablePrinter(_tabelModel, _Title);
  *     aPrinter.setOrientation(SLTablePrinter.LANDSCAPE); // or SLTablePrinter.PORTRAIT, or SLTablePrinter.REVERSE_LANDSCAPE
  *     aPrinter.setMargins(0.75, 0.75, 0.75, 0.75);
  *     //Format per Column
  *     aPrinter.addFormat( PMColumnManager.COLLATERAL, new DecimalFormat("#,###"));
  *     aPrinter.addFormat( PMColumnManager.REBATE, new DecimalFormat("#,##0.0000"));
  *     aPrinter.addFormat( PMColumnManager.FUND_RATE, new DecimalFormat("#,##0.0000"));
  *     aPrinter.print();
  *
  * 10. SLTablePrinter aPrinter = new SLTablePrinter(_tabelModel, _Title, _Orientation);
  *
  * </pre>
  */


import inetsoft.report.Margin;
import inetsoft.report.ReportEnv;
import inetsoft.report.StyleConstants;
import inetsoft.report.StyleFont;
import inetsoft.report.StyleSheet;
import inetsoft.report.j2d.StyleBook;
import inetsoft.report.j2d.StylePrinter;
import inetsoft.report.lens.AttributeTableLens;
import inetsoft.report.lens.swing.TableModelLens;

import java.awt.Color;
import java.awt.Component;
import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.table.TableModel;


public class SLTablePrinter implements Cancelable
{
  static { ReportEnv.setProperty("StyleReport.useCustomDriver", "true"); }

  public final static int PORTRAIT = PageFormat.PORTRAIT;
  public final static int LANDSCAPE = PageFormat.LANDSCAPE;
  public final static int REVERSE_LANDSCAPE = PageFormat.REVERSE_LANDSCAPE;

  private DBWaitDialog dbWaitDialog = null;
  private PrinterJob job = null;

  private SLTableView _tableView = null;
  private TableModel _tableModel = null;
  private String _title = null;
  private String _header = null;
  private int _titleAlignment = 0;
  private StyleSheet _report = null;
  private TableModelLens _lens = null;
  private boolean isTableSet = false;
  private boolean isHeaderSet = false;
  private int _orientation = SLTablePrinter.PORTRAIT;
  private Margin _margins = null;
  private boolean _fit2page = false;
  private int _fontSize = 7;
  private Hashtable _formats = new Hashtable(20);
  private Hashtable _strFormats = new Hashtable(200);
  private Hashtable _colFormats = new Hashtable(200);
  private Vector _beforeComponents = new Vector();
  private Vector _afterComponents = new Vector();
  private Vector _beforeObjects = new Vector();
  private Vector _afterObjects = new Vector();
  private boolean _initiated = false;
  private int _lastIndex = -1;

  public static SLTablePrinter createTablePrinter(TableModel aTM, String aTitle) {
    return new SLTablePrinter(aTM, aTitle);
  }

  public static SLTablePrinter createTablePrinter(TableModel aTM, String aTitle, int aOrient) {
    return new SLTablePrinter(aTM, aTitle, aOrient);
  }


  public SLTablePrinter(SLTablePrinter aTP)
  {
    _tableView = aTP._tableView;
    _tableModel = aTP._tableModel;
    _title = aTP._title;
    _header = aTP._header;
    _titleAlignment = aTP._titleAlignment;
    setOrientation(aTP._orientation);
    init();
  }

  public SLTablePrinter(TableModel aTM, String aTitle)
  {
    _tableView = null;
    _tableModel = aTM;
    _title = aTitle;
    _header = aTitle;
    init();
  }

  public SLTablePrinter(TableModel aTM, String aTitle, int aOrient)
  {
    _tableView = null;
    _tableModel = aTM;
    _title = aTitle;
    _header = aTitle;
    setOrientation(aOrient);
    init();
  }

  public SLTablePrinter(SLTableView aTV, String aTitle)
  {
    _tableView = aTV;
    _tableModel = null;
    _title = aTitle;
    _header = aTitle;
    init();
  }

  public SLTablePrinter(SLTableView aTV, String aTitle, int aOrient)
  {
    _tableView = aTV;
    _tableModel = null;
    _title = aTitle;
    _header = aTitle;
    setOrientation(aOrient);
    init();
  }

  public void print() {

/*
    if (_tableView != null) {
      _tableModel = _tableView.getModel();
      init();
    }else prepare();
*/
    updateProgress("Initializing ...");

    if (! _initiated ) {
        if (_tableView != null) { _tableModel = _tableView.getModel(); }
        init();
    }

    if (_report != null ) try {

      updateProgress("Printing ...");

      job = StylePrinter.getPrinterJob();
      PageFormat fmt = job.defaultPage();
      fmt.setOrientation(_orientation);
      StyleBook book = new StyleBook(_report, fmt);
      job.setPageable(book);
      job.print();

      updateProgress("Done.");

    }catch(Exception e) {e.printStackTrace();}
  }

  public void printWithDialog() {

/*
    if (_tableView != null) {
      _tableModel = _tableView.getModel();
      init();
    }else prepare();
*/
    updateProgress("Initializing ...");

    if (! _initiated ) {
        if (_tableView != null) { _tableModel = _tableView.getModel(); }
        init();
    }

    if (_report != null ) {

      updateProgress("Printing ...");

      job = StylePrinter.getPrinterJob();
      PageFormat aFmt = job.defaultPage();
      aFmt.setOrientation(_orientation);


      StyleBook book = new StyleBook(_report, aFmt);
      PageFormat fmt = job.pageDialog( book.getPageFormat(0) );
      book = new StyleBook(_report, fmt);
      job.setPageable(book);

      if(job.printDialog()) {
        try {
              job.print();
              updateProgress("Done.");
        } catch(Exception ex) {
          ex.printStackTrace();
        }
      }

    }
  }

  public boolean printAsyncWithDialog(JFrame _parent)
  {
    dbWaitDialog = new DBWaitDialog(
                                     _parent,
				     this,
				     new SwingWorker()
				       {
					 public Object construct()
					 {
					   try
					   {
					     printWithDialog();
					   }
					   catch( Exception ex )
					   {
					     return new Boolean(false);
					   }

                                           if (job.isCancelled())
                                              return new Boolean(false);

					   return new Boolean(true);
					 }

					 public void finished()
					 {
					   if( dbWaitDialog != null )
					   {
					     dbWaitDialog.dispose();
					   }
					 }
				       } );

    dbWaitDialog.setLabel("Printing. Please Wait...");

    Object ret = dbWaitDialog.showDialog();

    dbWaitDialog = null;
    job = null;

    return ((Boolean)ret).booleanValue();
  }

  public boolean printAsyncWithDialog(JDialog _parent)
  {
    dbWaitDialog = new DBWaitDialog(
                                     _parent,
				     this,
				     new SwingWorker()
				       {
					 public Object construct()
					 {
					   try
					   {
					     printWithDialog();
					   }
					   catch( Exception ex )
					   {
					     return new Boolean(false);
					   }

                                           if (job.isCancelled())
                                              return new Boolean(false);

					   return new Boolean(true);
					 }

					 public void finished()
					 {
					   if( dbWaitDialog != null )
					   {
					     dbWaitDialog.dispose();
					   }
					 }
				       } );

    dbWaitDialog.setLabel("Printing. Please Wait...");

    Object ret = dbWaitDialog.showDialog();

    dbWaitDialog = null;
    job = null;

    return ((Boolean)ret).booleanValue();
  }

  public boolean printAsync(JFrame _parent)
  {
    dbWaitDialog = new DBWaitDialog(
                                     _parent,
				     this,
				     new SwingWorker()
				       {
					 public Object construct()
					 {
					   try
					   {
					     print();
					   }
					   catch( Exception ex )
					   {
					     return new Boolean(false);
					   }

                                           if (job != null && job.isCancelled())
                                              return new Boolean(false);

					   return new Boolean(true);
					 }

					 public void finished()
					 {
					   if( dbWaitDialog != null )
					   {
					     dbWaitDialog.dispose();
					   }
					 }
				       } );

    dbWaitDialog.setLabel("Printing. Please Wait...");

    Object ret = dbWaitDialog.showDialog();

    dbWaitDialog = null;
    job = null;

    return ((Boolean)ret).booleanValue();
  }

  public boolean printAsync(JDialog _parent)
  {
    dbWaitDialog = new DBWaitDialog(
                                     _parent,
				     this,
				     new SwingWorker()
				       {
					 public Object construct()
					 {
					   try
					   {
					     print();
					   }
					   catch( Exception ex )
					   {
					     return new Boolean(false);
					   }

                                           if (job.isCancelled())
                                              return new Boolean(false);

					   return new Boolean(true);
					 }

					 public void finished()
					 {
					   if( dbWaitDialog != null )
					   {
					     dbWaitDialog.dispose();
					   }
					 }
				       } );

    dbWaitDialog.setLabel("Printing. Please Wait...");

    Object ret = dbWaitDialog.showDialog();

    dbWaitDialog = null;
    job = null;

    return ((Boolean)ret).booleanValue();
  }

  public void cancel()
  {
    if (job != null)
    {
        try {
               job.cancel();
        }
        catch(Exception ex)
        {
            Util.error("[SLTablePrinter] printing " + ((_title == null) ? "anonymous" : _title) + " caught: " + ex.getMessage() );
        }
    }
  }

  private void init() {
    _report = StyleSheet.createStyleSheet();
    _report.setMargin( (_margins != null) ? _margins : new Margin(0.9, 0.5, 0.7, 0.5) );
    _report.setHeaderFromEdge(0.65);
    _report.setFooterFromEdge(0.35);
    _report.setCurrentAlignment(StyleConstants.H_LEFT);
    isTableSet = isHeaderSet = false;

    _report.setCurrentTableLayout( (_fit2page) ? StyleSheet.TABLE_FIT_PAGE : StyleSheet.TABLE_FIT_CONTENT_1PP);

    prepare();

    _report.setCurrentAlignment(StyleConstants.H_RIGHT);
    _report.setCurrentFont(new StyleFont("Dialog", StyleFont.PLAIN, 9));
    _report.addFooterText("Page {P} of {N}");
    _report.setCurrentAlignment(StyleConstants.H_CENTER);
    _report.setCurrentFont(new StyleFont("Dialog", StyleFont.PLAIN, 8));
  }

  public void setTableModel(TableModel aTM) { _tableModel = aTM; _tableView = null; _initiated = false;}
  public TableModel getTableModel() {return _tableModel;}

  public void setTableView(SLTableView aTV) { _tableView = aTV; _tableModel = null; _initiated = false;}
  public SLTableView getTableView() {return _tableView;}

  public void setHeader(String aT) { _header = aT; _initiated = false;}
  public String getHeader() {return _header;}

  public void setTitle(String aT, int align) { _title = aT; _titleAlignment = align; _initiated = false;}
  public void setTitle(String aT) { _title = aT; _initiated = false;}
  public String getTitle() {return _title;}

  public Margin getMargins() {return _margins; }
  public void setMargin(Margin aM) {_margins = aM; _initiated = false;}
  public void setMargin(double top, double left, double bottom, double right) {
    // adjustment for header and footer
    top = (top < 0.9) ? 0.9 : top;
    bottom = (bottom < 0.7) ? 0.7 : bottom;

    _margins = new Margin(top, left, bottom, right);
    _initiated = false;
  }

  public int getOrientation() {return _orientation;}
  public void setOrientation(int aI) {
      switch (aI) {
          case SLTablePrinter.LANDSCAPE:
          case SLTablePrinter.PORTRAIT:
          case SLTablePrinter.REVERSE_LANDSCAPE:
               _orientation = aI;
               _initiated = false;
               break;
      }
  }

  public void setFit2Page(boolean aB) {_fit2page = aB; _initiated = false;}
  public void setFontSize(int aI) {_fontSize = aI; _initiated = false;}

  public void addFormat(Class type, Format aFrm) { _formats.put(type, aFrm); _initiated = false;}
  public void removeFormat(Class type)           { _formats.remove(type); _initiated = false;}

  public void addFormat(String colName, Format aFrm) { _strFormats.put(colName, aFrm); _initiated = false;}
  public void removeFormat(String colName)           { _strFormats.remove(colName); _initiated = false;}

  public void addFormat(int aCol, Format aFrm) { _colFormats.put(new Integer(aCol), aFrm); _initiated = false;}
  public void removeFormat(int aCol)           { _colFormats.remove(new Integer(aCol)); _initiated = false;}

  public void addBeforeTable(Component aC) {_beforeComponents.add(aC); _initiated = false;}
  public void removeBefore(Component aC) {_beforeComponents.remove(aC); _initiated = false;}
  public void addAfterTable(Component aC) {_afterComponents.add(aC); _initiated = false;}
  public void removeAfter(Component aC) {_afterComponents.remove(aC); _initiated = false;}

  public void addBeforeTable(Object aO) {_beforeObjects.add(aO); _initiated = false;}
  public void removeBefore(Object aO) {_beforeObjects.remove(aO); _initiated = false;}
  public void addAfterTable(Object aO) {_afterObjects.add(aO); _initiated = false;}
  public void removeAfter(Object aO) {_afterObjects.remove(aO); _initiated = false;}


  public StyleSheet getReportAfterPrint() {
    if (_tableView != null) {
      _tableModel = _tableView.getModel();
    }

    init();

    _initiated = true;

    return _report;
  }

  private void prepare() {
    if (!isHeaderSet ) {
        if (_header != null && _header.length() > 0) {
          _report.setCurrentFont(new StyleFont("Dialog", StyleFont.BOLD, 10));
          _report.setCurrentAlignment(StyleConstants.H_LEFT);
          _report.addHeaderText(_header);
        }

        _report.setCurrentFont(new StyleFont("Dialog", StyleFont.ITALIC, 9));
        _report.setCurrentAlignment(StyleConstants.H_RIGHT);
        _report.addHeaderText("  {D,MMMM dd, yyyy HH:MM}");

        _report.setCurrentSeparatorAdvance(1);
        _report.setCurrentForeground(Color.black);
        _report.addHeaderSeparator(StyleConstants.THIN_LINE);

        isHeaderSet = true;
    }

    //_report.addTable(new Grid7(_lens));
    _report.setCurrentFont(new StyleFont("Dialog", StyleFont.PLAIN, _fontSize));

    _report.addFormat(Date.class, new SimpleDateFormat( "dd-MMM-yyyy" ));
    _report.addFormat(Long.class, new DecimalFormat("#,##0"));
    _report.addFormat(Double.class, new DecimalFormat("#,##0.00"));
    //_report.addFormat(String.class, new StFormat("#,##0.00"));

    for(Enumeration en = _formats.keys(); en.hasMoreElements();) {
        Class aC = (Class) en.nextElement();
        Format aF = (Format) _formats.get(aC);
        _report.addFormat( aC , aF );
    }

    if (_title != null && _title.length() > 0) {
        _report.setCurrentFont( new StyleFont("Dialog", StyleFont.BOLD, 10) );
        _report.setCurrentAlignment( (_titleAlignment == 0) ? StyleConstants.H_CENTER : _titleAlignment);
        _report.addText(_title);
        _report.addBreak();
    }

    _report.setCurrentFont(new StyleFont("Dialog", StyleFont.PLAIN, _fontSize));

    for(Enumeration en = _beforeComponents.elements(); en.hasMoreElements();) {
        Component aC = (Component) en.nextElement();
        _report.addComponent( aC );
    }

    for(Enumeration en = _beforeObjects.elements(); en.hasMoreElements();) {
        Object aO = (Object) en.nextElement();
        _report.addObject( aO );
    }


    if (!isTableSet && _tableModel != null) {
      _lens = new TableModelLens(_tableModel);
      _lens.setAlignment(StyleConstants.H_LEFT);
      _lens.setColAutoSize(true);
      _lens.setRowAutoSize(true);

      for(Enumeration en = _strFormats.keys(); en.hasMoreElements();) {
        String aS = (String) en.nextElement();
        Format aF = (Format) _strFormats.get(aS);
        _lens.setFormat( aS , aF );
      }

      SLTableModel tm = ((SLTableModel) _tableModel);
      SLColumnDefinition aCL;
      for(Enumeration en = _colFormats.keys(); en.hasMoreElements();) {
        Integer aI = (Integer) en.nextElement();
        Format aF = (Format) _colFormats.get(aI);

        aCL = tm.getById(aI.intValue());

        if (aCL != null && tm.getColumnIndex(aI.intValue()) > -1)
           _lens.setFormat( aCL.getDisplayLabel() , aF );
      }

      _lens.setColBorderColor(Color.gray);
      _lens.setRowBorderColor(Color.gray);

      AttributeTableLens attr = new AttributeTableLens(_lens);
      attr.setColBorderColor(Color.gray);
      attr.setRowBorderColor(Color.gray);
      attr.setAlignment(StyleConstants.H_RIGHT);

      _report.setCurrentAlignment(StyleConstants.H_LEFT);
      //_report.addTable( _lens );
      _report.addTable( attr );
      isTableSet = true;
    }

    for(Enumeration en = _afterComponents.elements(); en.hasMoreElements();) {
        Component aC = (Component) en.nextElement();
        _report.addComponent( aC );
    }

    for(Enumeration en = _afterObjects.elements(); en.hasMoreElements();) {
        Object aO = (Object) en.nextElement();
        _report.addObject( aO );
    }

  }

  public void print2report(StyleSheet aReport) {
    TableModelLens aLens = null;

    aReport.setMargin( (_margins != null) ? _margins : new Margin(0.9, 0.5, 0.7, 0.5) );
    aReport.setHeaderFromEdge(0.7);
    aReport.setFooterFromEdge(0.35);

    aReport.setCurrentTableLayout( (_fit2page) ? StyleSheet.TABLE_FIT_PAGE : StyleSheet.TABLE_FIT_CONTENT_1PP);

    if (_title != null && _title.length() > 0) {
          aReport.setCurrentFont(new StyleFont("Dialog", StyleFont.BOLD, 10));
          aReport.setCurrentAlignment(StyleConstants.H_LEFT);
          aReport.addText(_title);
          aReport.setCurrentFont(new StyleFont("Dialog", StyleFont.ITALIC, 9));
          aReport.setCurrentAlignment(StyleConstants.H_RIGHT);
          aReport.addText("  {D,MMMM dd, yyyy HH:MM}");
          aReport.addSeparator(StyleConstants.THIN_LINE);
          // aReport.addNewline(1);
    }else {
          aReport.setCurrentFont(new StyleFont("Dialog", StyleFont.ITALIC, 9));
          aReport.setCurrentAlignment(StyleConstants.H_RIGHT);
          aReport.addText("  {D,MMMM dd, yyyy HH:MM}");
          aReport.addSeparator(StyleConstants.THIN_LINE);
    }

    //aReport.addTable(new Grid7(_lens));
    aReport.setCurrentFont(new StyleFont("Dialog", StyleFont.PLAIN, _fontSize));

    aReport.addFormat(Date.class, new SimpleDateFormat( "dd-MMM-yyyy" ));
    aReport.addFormat(Long.class, new DecimalFormat("#,##0"));
    aReport.addFormat(Double.class, new DecimalFormat("#,##0.00"));

    for(Enumeration en = _formats.keys(); en.hasMoreElements();) {
        Class aC = (Class) en.nextElement();
        Format aF = (Format) _formats.get(aC);
        aReport.addFormat( aC , aF );
    }

    for(Enumeration en = _beforeComponents.elements(); en.hasMoreElements();) {
        Component aC = (Component) en.nextElement();
        aReport.addComponent( aC );
    }

    for(Enumeration en = _beforeObjects.elements(); en.hasMoreElements();) {
        Object aO = (Object) en.nextElement();
        aReport.addObject( aO );
    }


    if (_tableModel != null) {
      aLens = new TableModelLens(_tableModel);
      aLens.setColAutoSize(true);
      aLens.setRowAutoSize(true);

      for(Enumeration en = _strFormats.keys(); en.hasMoreElements();) {
        String aS = (String) en.nextElement();
        Format aF = (Format) _strFormats.get(aS);
        aLens.setFormat( aS , aF );
      }

      SLColumnDefinition aCL;
      for(Enumeration en = _colFormats.keys(); en.hasMoreElements();) {
        Integer aI = (Integer) en.nextElement();
        Format aF = (Format) _colFormats.get(aI);
        aCL = ((SLTableModel) _tableModel).getById(aI.intValue());
        aLens.setFormat( aCL.getDisplayLabel() , aF );
      }

      aLens.setColBorderColor(Color.gray);
      aLens.setRowBorderColor(Color.gray);

      AttributeTableLens attr = new AttributeTableLens(aLens);
      attr.setColBorderColor(Color.gray);
      attr.setRowBorderColor(Color.gray);
      attr.setAlignment(StyleConstants.H_RIGHT);

      aReport.setCurrentAlignment(StyleConstants.H_LEFT);
      //aReport.addTable( aLens );
      aReport.addTable( attr );
    }

    for(Enumeration en = _afterComponents.elements(); en.hasMoreElements();) {
        Component aC = (Component) en.nextElement();
        aReport.addComponent( aC );
    }

    for(Enumeration en = _afterObjects.elements(); en.hasMoreElements();) {
        Object aO = (Object) en.nextElement();
        aReport.addObject( aO );
    }


    aReport.setCurrentAlignment(StyleConstants.H_RIGHT);
    aReport.setCurrentFont(new StyleFont("Dialog", StyleFont.PLAIN, 9));
    aReport.addFooterText("Page {P} of {N}");
    aReport.setCurrentAlignment(StyleConstants.H_CENTER);
    aReport.setCurrentFont(new StyleFont("Dialog", StyleFont.PLAIN, 8));

  }

  protected void updateProgress( final String note )
  {
    if( dbWaitDialog == null )
      return;

    Runnable doUpdateProgress = new Runnable()
      {
	public void run()
	{
	  dbWaitDialog.setNote( note );
	}
      };
    SwingUtilities.invokeLater( doUpdateProgress );
  }

}